<template>
  <div class="login">
    <el-card class="box-card">
      <h4>管理员登录窗口</h4>
      <el-form
        :model="loginForm"
        status-icon
        :rules="rules"
        ref="loginForm"
        label-width="60px"
        class="demo-loginForm"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            type="text"
            v-model="loginForm.username"
            autocomplete="on"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            v-model="loginForm.password"
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm('loginForm')"
            >提交</el-button
          >
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import api from "../api";
import { mapMutations } from "vuex";

export default {
  name: "Login",
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.loginForm.checkPass !== "") {
          this.$refs.loginForm.validateField("checkPass");
        }
        callback();
      }
    };

    var validateUser = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入用户名"));
      } else {
        if (this.loginForm.checkPass !== "") {
          this.$refs.loginForm.validateField("checkPass");
        }
        callback();
      }
    };

    return {
      loginForm: {
        username: "",
        password: "",
      },
      rules: {
        username: [{ validator: validateUser, trigger: "blur" }],
        password: [{ validator: validatePass, trigger: "blur" }],
      },
    };
  },
  methods: {
    ...mapMutations("admain", ["setAdmainName"]),

    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        //  console.log(valid);
        if (valid) {
          // 发送数据请求
          let username = this.loginForm.username;
          let password = this.loginForm.password;
          api
            .login({ username, password })
            .then((res) => {
              if (res.status == 200) {
                //成功
                //存到本地
                localStorage.setItem("admainName", username);
                //存到仓库
                this.setAdmainName(username);

                // token
                // this.$store.commit("LOGIN_IN", res.data.data.token);

                //跳转到首页
                this.$router.push("/");
              }
            })
            .catch((error) => {
              this.$message.error(error.data.errors);
              //清空历史记录
              this.loginForm = {
                username: "",
                password: "",
              };
            });
        } else {
          return false;
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login {
  .box-card {
    margin: 50px auto;
    width: 480px;
  }

  h4 {
    margin-bottom: 20px;
  }
}
</style>>

